

package c.x.jy.service.redis;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils; 

 
 
 

/**
 * Redis工具类
 *
 */
@Component
public class RedisUtils {
	

	
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
     private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;
    @Autowired
    private ZSetOperations<String, Object> zSetOperations;
    

	 
    @Autowired
    private  RedisConnection redisConnection;
    /**  默认过期时长，单位：秒 */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**  不设置过期时长 */
    public final static long NOT_EXPIRE = -1;
  
    public static RedisUtils redisUtils;
    
    public static Cache cache;
    @Autowired
    private   Cache c;
    @PostConstruct
   public   void init() {
	  redisUtils=this;
	  
	  cache=c;
   }
    public void set(String key, Object value, long expire){
        valueOperations.set(key, toJson(value));
           
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
      
    }

    public void set(String key, Object value){
        set(key, value, DEFAULT_EXPIRE);
    }

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
    	if(key==null)return null;
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public void delete(String key) {
        Set<String> keys =   redisTemplate.keys(key ) ;
        if(keys!=null)    redisTemplate.delete(keys);
		  keys = redisTemplate.keys(key + "*");
        if(keys!=null)   redisTemplate.delete(keys);
    }
    public Set<String> keys(String key){
    	return  redisTemplate.keys(key + "*");
    }
    
	public void del(String cache, String key) {
     System.out.println("删除缓存 ："+cache+":"+key);
		 
		Set<String> keys = null;
		if (StringUtils.isNotEmpty(key)) {
			keys = redisTemplate.keys(cache + ":" + key + "*");
		} else {
			keys = redisTemplate.keys(cache + ":" + "*");

		}
		if(keys!=null)
		redisTemplate.delete(keys);

	}
	
	
 
	

    /**
     * Object转成JSON数据
     */
    private String toJson(Object object){
        if(object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean || object instanceof String){
            return String.valueOf(object);
        }
        return JSON.toJSONString(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz){
        return   JSON.parseObject(json,clazz)  ;
    }
    
    /**
                * 自增
     * @param key
     * @param i
     * @return
     */
	public Long setIncr(String key, Long i) {
		valueOperations.size(key);

		return redisConnection.incrBy(key.getBytes() ,i);
	}	
}
